package com.example.pc.underlinetextview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.text.Layout;
import android.util.AttributeSet;
import android.util.TypedValue;

/**
 * Created by pc on 2017/10/12.
 */

public class UnderLineTextView extends android.support.v7.widget.AppCompatTextView {
    /**
     * 下划线宽度.
     */
    private int mUnderlineWidth;
    /**
     * 下划线颜色.
     */
    private int mUnderlineColor =0xffff;
    /**
     * 画笔.
     */
    private Paint mPaint;
    /**
     * 文字显示矩形区域.
     */
    private Rect mRect;

    public UnderLineTextView(Context context) {
        this(context,null);
    }

    public UnderLineTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public UnderLineTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.UnderLineTextView, defStyleAttr, 0);
        mUnderlineWidth = (int) typedArray.getDimension(R.styleable.UnderLineTextView_underlineWidth,dpToPx(1));
        mUnderlineColor =typedArray.getColor(R.styleable.UnderLineTextView_underlineColor, mUnderlineColor);
        typedArray.recycle();

        //设置画笔
        mRect=new Rect();
        mPaint=new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mUnderlineWidth);
        mPaint.setColor(mUnderlineColor);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //获取行数
        int lineCount=getLineCount();
        //获取textview布局
        Layout layout = getLayout();
        float startX,stopX,diffX;
        int firstCharInLine,lastCharInLine;
        for (int i = 0; i < lineCount; i++) {

            //getLineBounds得到这一行的外包矩形,
            // 这个字符的顶部Y坐标就是rect的top 底部Y坐标就是rect的bottom
            int baseline=getLineBounds(i,mRect);
            firstCharInLine=layout.getLineStart(i);
            lastCharInLine = layout.getLineEnd(i);

            //要得到这个字符的左边X坐标 用layout.getPrimaryHorizontal
            //得到字符的右边X坐标用layout.getSecondaryHorizontal
            startX = layout.getPrimaryHorizontal(firstCharInLine);
            diffX = layout.getPrimaryHorizontal(firstCharInLine + 1) - startX;
            stopX = layout.getPrimaryHorizontal(lastCharInLine - 1) + diffX;

            canvas.drawLine(startX,baseline + mUnderlineWidth,stopX, baseline + mUnderlineWidth, mPaint);

        }


    }

    @Override
    public void setPadding(int left, int top, int right, int bottom) {
        super.setPadding(left, top, right, bottom+mUnderlineWidth);
    }

    private float dpToPx(int i) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,i,getResources().getDisplayMetrics());
    }

    public int getUnderLineColor() {
        return mUnderlineColor;
    }

    public void setUnderLineColor(int color) {
        this.mUnderlineColor = color;
        invalidate();
    }

    public float getmUnderlineWidth() {
        return mUnderlineWidth;
    }

    public void setmUnderlineWidth(int underlineWidth) {
        this.mUnderlineWidth = underlineWidth;
        invalidate();
    }
}
